package top.ucat.starter.log.common.beans;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @Auther: Jun
 * @Date: 2019/11/19 12:23
 * @Description:
 */
public class LogParmThreadLocal {

    public static final ThreadLocal<HashMap<String, String>> LOCAL = new ThreadLocal<HashMap<String, String>>();

    public static void set(String key, String value) {
        setDefault();
        if (StringUtils.isNoneBlank(key)) {
            LOCAL.get().put(key, value);
        }
    }

    public static String get(String key) {
        setDefault();
        if (StringUtils.isNoneBlank(key)) {
            return LOCAL.get().get(key);
        }
        return null;
    }

    public static HashMap<String, String> getMap() {
        setDefault();
        return LOCAL.get();
    }

    static void setDefault() {
        if (LOCAL.get() == null) {
            synchronized (LogParmThreadLocal.class) {
                if (LOCAL.get() == null) {
                    LOCAL.set(new HashMap<String, String>());
                }
            }
        }
    }
}
